package com.ddstation.common.controller;

import com.ddstation.common.service.ImageService;
import com.ddstation.common.util.AliCloudOssUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import javax.inject.Inject;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller @Lazy
public class ImgUploadController {

	public static String userDir = "Uploads/Picture/workorder";
	public static String userDirChat = "Uploads/Picture/chat";
	public static String doctorDir = "Uploads/Picture/doctor_upload_img";
	public static String publicDirQr = "xcx-img/qrcode";
	public static String publicBucket = "wenuan-oss-public";
	
	@Inject
	private ImageService imageService;
	
	private static final Logger logger =
            LoggerFactory.getLogger(ImgUploadController.class);
	
	//图片上传
	@RequestMapping(value="/appinvs_imgUpload")
	public @ResponseBody Map<String, Object> imgUploadEvent(
			MultipartHttpServletRequest request) throws IOException {
		
		Map<String, Object> retMap = new HashMap<String, Object>();
		retMap.put("errCode", -1);
		
	    	SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
	    	String today = sdf.format(new Date());
    	
        //上传路径 
    		String outpath = System.getProperty("user.dir") + doctorDir + "/" + today;


		List<MultipartFile> files = request.getFiles("myfiles");
		String myFileName = request.getParameter("fileName");
		
		for(MultipartFile file:files) {
			File localFile = new File(outpath);
			if(!localFile.isDirectory())
				localFile.mkdirs();
            localFile = new File(localFile, myFileName + "_" + file.getOriginalFilename());
            file.transferTo(localFile);
            
            // 阿里云上传 object key
            String ossKey = doctorDir + "/" + today + "/" + myFileName + file.getOriginalFilename().substring(0,file.getOriginalFilename().lastIndexOf(".")) + file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf("."));
            // 上传 阿里云 oss
            AliCloudOssUtil.putOSSObject(null, ossKey, localFile);
            
	    		// 图片数据to数据库
	    		int imageId = imageService.addImage(ossKey);
	    		localFile.delete();
	    		// 获取图片访问url
	    		retMap.put("imgId", imageId);
	    		retMap.put("errCode", 0);
		}
		
		return retMap;
	}
	
	//医生二维码图片上传
	@RequestMapping(value="/wnxcx_doctorQrCodeImgUpload")
	public @ResponseBody Map<String, Object> doctorQrCodeImgUploadEvent(
			MultipartHttpServletRequest request) throws IOException {
		
		Map<String, Object> retMap = new HashMap<String, Object>();
		retMap.put("errCode", -1);
		
	    	SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
	    	String today = sdf.format(new Date());
    	
        //上传路径 
    		String outpath = System.getProperty("user.dir") + doctorDir + "/" + today;
    	
        File localFile = new File(outpath);
        if(!localFile.isDirectory())
        	localFile.mkdirs();
		
		List<MultipartFile> files = request.getFiles("myfiles");
		String myFileName = request.getParameter("fileName");
		
		for(MultipartFile file:files) {
            localFile = new File(localFile, myFileName + "_" + file.getOriginalFilename());
            file.transferTo(localFile);
            
            // 阿里云上传 object key
            String ossKey = publicDirQr + "/" + myFileName + file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf("."));
            // 上传 阿里云 oss
            AliCloudOssUtil.putOSSObject(publicBucket, ossKey, localFile);
            
	    		// 获取图片访问url
	    		retMap.put("imgPath", ossKey);
	    		retMap.put("errCode", 0);
		}
		
		return retMap;
	}
	
	//图片上传
	@RequestMapping(value="/wnxcx_workImgUpload")
	public @ResponseBody Map<String, Object> workImgUploadEvent(
			MultipartHttpServletRequest request) throws IOException {
		
		Map<String, Object> retMap = new HashMap<String, Object>();
		retMap.put("errCode", -1);
		
	    	SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
	    	String today = sdf.format(new Date());
    	
        //上传路径 
    		String outpath = System.getProperty("user.dir") + userDir + "/" + today;
    	
        File localFile = new File(outpath);
        if(!localFile.isDirectory())
        	localFile.mkdirs();
		
		List<MultipartFile> files = request.getFiles("myfiles");
		String myFileName = request.getParameter("fileName");

		for(MultipartFile file:files) {
            localFile = new File(localFile, myFileName + "_" + file.getOriginalFilename());
            file.transferTo(localFile);
            
            // 阿里云上传 object key
            String ossKey = userDir + "/" + today + "/" + myFileName + "_" + file.getOriginalFilename();
            // 上传 阿里云 oss
            AliCloudOssUtil.putOSSObject(null, ossKey, localFile);
            
    		// 图片数据to数据库
    		int imageId = imageService.addImage(ossKey);
    		localFile.delete();
    		
    		// 获取图片访问url
    		retMap.put("imgId", imageId);
    		retMap.put("errCode", 0);
		}
		
		return retMap;
	}

	//App图片上传
	@RequestMapping(value="/qztapp_workImgUpload")
	public @ResponseBody Map<String, Object> qztAppWorkImgUploadEvent(
			MultipartHttpServletRequest request) throws IOException {

		Map<String, Object> retMap = new HashMap<String, Object>();
		retMap.put("errCode", -1);

		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		String today = sdf.format(new Date());

		//上传路径
		String outpath = System.getProperty("user.dir") + userDir + "/" + today;


		List<MultipartFile> files = request.getFiles("myfiles");
		String myFileName = request.getParameter("fileName");
		String imageIds = "";
		for(MultipartFile file:files) {
			File localFile = new File(outpath);
			if(!localFile.isDirectory())
				localFile.mkdirs();
			localFile = new File(localFile, myFileName + "_" + file.getOriginalFilename());
			file.transferTo(localFile);

			// 阿里云上传 object key
			String ossKey = userDir + "/" + today + "/" + myFileName + "_" + file.getOriginalFilename();
			// 上传 阿里云 oss
			AliCloudOssUtil.putOSSObject(null, ossKey, localFile);

			// 图片数据to数据库
			int imageId = imageService.addImage(ossKey);
			localFile.delete();
			// 获取图片访问url
			imageIds = imageIds + imageId + ",";
		}
		if (imageIds.length()>0) {
			imageIds = imageIds.substring(0,imageIds.length()-1);
			retMap.put("imgId", imageIds);
			retMap.put("errCode", 0);
		}
		return retMap;
	}
	
	//聊天室图片上传
	@RequestMapping(value="/wnxcx_chatImgUpload")
	public @ResponseBody Map<String, Object> chatImgUploadEvent(
			MultipartHttpServletRequest request) throws IOException {
		
		Map<String, Object> retMap = new HashMap<String, Object>();
		retMap.put("errCode", -1);
		
	    	SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
	    	String today = sdf.format(new Date());
    	
        //上传路径 
	    	String outpath = System.getProperty("user.dir") + userDirChat + "/" + today;
    	
        File localFile = new File(outpath);
        if(!localFile.isDirectory())
        	localFile.mkdirs();
		
		List<MultipartFile> files = request.getFiles("myfiles");
		String myFileName = request.getParameter("fileName");

		for(MultipartFile file:files) {
            localFile = new File(localFile, myFileName + "_" + file.getOriginalFilename());
            file.transferTo(localFile);
            
            logger.debug("压缩前:"+localFile.getName()+"_"+String.valueOf(localFile.length()));
        	
            // 阿里云上传 object key
            String ossKey = userDirChat + "/" + today + "/" + myFileName + "_" + file.getOriginalFilename();
            // 上传 阿里云 oss
            AliCloudOssUtil.putOSSObject(null, ossKey, localFile);
            localFile.delete();
    		// 获取图片访问url
    		retMap.put("ossKey", ossKey);
    		retMap.put("errCode", 0);
		}
		
		return retMap;
	}
	
}

